﻿#include "globalstatic.h"

#include <QDebug>
#include <QDateTime>

// 静态变量初始化
qint64 GlobalStatic::g_zeroTimeStamp = 0;
int GlobalStatic::g_gpioResetStatus = 0;
bool GlobalStatic::g_sining = false;
double GlobalStatic::g_sinT = 1;
double GlobalStatic::g_sinA = 1;
qint64 GlobalStatic::g_sinZeroTimeStamp = 0;
qint64 GlobalStatic::g_sinDestTimeStamp = 0;
bool GlobalStatic::g_sinSquare = false;

QMap<QString, DataType> GlobalStatic::g_dataTypeMap;
qint32 GlobalStatic::g_dataLength = 0;
QMap<QString, QByteArray> GlobalStatic::g_cmdMap;
QMap<QString, QString> GlobalStatic::g_trainStepMap;

// 训练考试是否已经开始及开始时间
bool GlobalStatic::g_training = false;
qint64 GlobalStatic::g_trainStartTime = 0;

quint16 GlobalStatic::g_sendFrameCount = 0;
QByteArray GlobalStatic::g_planCode(4, 0x0);

// 科目编号
QString GlobalStatic::g_courseNumber = "C15101";
// 故障数量
quint16 GlobalStatic::g_faultCount = 0;
// 注入的故障码
QStringList GlobalStatic::g_faultCodes;
// 故障得分
QMap<QString, double> GlobalStatic::g_faultScoreMap;
// 记录流程步骤数目
QMap<QString, bool> GlobalStatic::g_stepTrainedMap;

GlobalStatic::GlobalStatic()
{
}

qint64 GlobalStatic::getZeroTimeStamp()
{
    return g_zeroTimeStamp;
}

void GlobalStatic::setZeroTimeStamp(const qint64 zeroTimeStamp)
{
    g_zeroTimeStamp = zeroTimeStamp;
}

int GlobalStatic::getGPIOResetStatus()
{
    return g_gpioResetStatus;
}

void GlobalStatic::setGPIOResetStatus(const int status)
{
    g_gpioResetStatus = status;
}

bool GlobalStatic::getSining()
{
    return g_sining;
}

double GlobalStatic::getSinT()
{
    return g_sinT;
}

double GlobalStatic::getSinA()
{
    return g_sinA;
}

qint64 GlobalStatic::getSinZeroTimeStamp()
{
    return g_sinZeroTimeStamp;
}

qint64 GlobalStatic::getSinDestTimeStamp()
{
    return g_sinDestTimeStamp;
}

bool GlobalStatic::getSinSquare()
{
    return g_sinSquare;
}

void GlobalStatic::setSinStart(const double sinT, const double sinA, const int tcount, bool sinSquare)
{
    g_sining = true;
    g_sinT = sinT;
    g_sinA = sinA;
    g_sinZeroTimeStamp = QDateTime::currentMSecsSinceEpoch();
    g_sinDestTimeStamp = g_sinZeroTimeStamp + sinT * tcount * 1000;
    g_sinSquare = sinSquare;
}

void GlobalStatic::setSinStop()
{
    g_sining = false;
    g_sinSquare = false;
}

void GlobalStatic::beforeSining()
{
    g_sining = false;
    g_sinT = 1;
    g_sinA = 1;
    g_sinZeroTimeStamp = g_zeroTimeStamp;
    g_sinDestTimeStamp = g_zeroTimeStamp;
}

DataType GlobalStatic::getDataType(const QString dataType)
{
    return g_dataTypeMap.value(dataType);
}

void GlobalStatic::initDataType()
{
    g_dataTypeMap.insert("uc", UC);
    g_dataTypeMap.insert("sc", SC);
    g_dataTypeMap.insert("us", US);
    g_dataTypeMap.insert("ss", SS);
    g_dataTypeMap.insert("ui", UI);
    g_dataTypeMap.insert("si", SI);
    g_dataTypeMap.insert("ul", UL);
    g_dataTypeMap.insert("sl", SL);
    g_dataTypeMap.insert("f32", F32);
    g_dataTypeMap.insert("f", F);
    g_dataTypeMap.insert("f64", F64);
    g_dataTypeMap.insert("dbl", DBL);
}

qint32 GlobalStatic::getDataLength()
{
    return g_dataLength;
}

void GlobalStatic::setDataLength(const qint32 dataLength)
{
    g_dataLength = dataLength;
}

QByteArray GlobalStatic::getCmd(const QString &name)
{
    if (g_cmdMap.contains(name))
        return g_cmdMap.value(name);

    return QByteArray(0x0);
}

void GlobalStatic::initCmd(const QList<Cmd> &cmdList)
{
    for (int i = 0; i < cmdList.size(); i++) {
        Cmd cmd = cmdList.at(i);
        QString cmdString = cmd.cmd.remove(QRegExp("\\s"));
        g_cmdMap.insert(cmd.name, QByteArray::fromHex(cmdString.toLatin1()));
    }
}

QString GlobalStatic::getTrainStep(const QString &stepCode)
{
    if (g_trainStepMap.contains(stepCode))
        return g_trainStepMap.value(stepCode);

    return QString("");
}

void GlobalStatic::initTrainStep(const QList<TrainStep> &trainStepList)
{
    for (int i = 0; i < trainStepList.size(); i++) {
        TrainStep trainStep = trainStepList.at(i);
        g_trainStepMap.insert(trainStep.stepCode, trainStep.stepName);
    }
}

quint16 GlobalStatic::sendFrameCount()
{
    return g_sendFrameCount;
}

void GlobalStatic::increaseSendFrameCount()
{
    g_sendFrameCount++;
}

bool GlobalStatic::training()
{
    return g_training;
}

void GlobalStatic::setTraining(const bool training)
{
    g_training = training;
}

qint64 GlobalStatic::trainStartTime()
{
    return g_trainStartTime;
}

void GlobalStatic::setTrainStartTime(const qint64 &trainStartTime)
{
    g_trainStartTime = trainStartTime;
}

QByteArray GlobalStatic::planCode()
{
    return g_planCode;
}

void GlobalStatic::setPlanCode(const QByteArray &planCode)
{
    if (planCode.size() >= 4) {
        for (int i = 0; i < 4; i++)
            g_planCode[i] = planCode.at(i);
    }
}

QString GlobalStatic::courseNumber()
{
    return g_courseNumber;
}

void GlobalStatic::setCourseNumber(const QString &courseNumber)
{
    g_courseNumber = courseNumber;
}

quint16 GlobalStatic::faultCount()
{
    return g_faultCount;
}

void GlobalStatic::setFaultCount(const quint16 &faultCount)
{
    g_faultCount = faultCount;
}

QStringList GlobalStatic::faultCodes()
{
    return g_faultCodes;
}

bool GlobalStatic::containFaultCode(const QString &faultCode)
{
    return g_faultCodes.contains(faultCode);
}

void GlobalStatic::clearFaultCode()
{
    g_faultCodes.clear();
}

void GlobalStatic::appendFaultCode(const QString &faultCode)
{
    g_faultCodes.append(faultCode);
}

double GlobalStatic::faultScore(const QString faultCode)
{
    return g_faultScoreMap.value(faultCode);
}

void GlobalStatic::clearFaultScore()
{
    g_faultScoreMap.clear();
}

void GlobalStatic::setFaultScore(const QString faultCode, const double faultScore)
{
    g_faultScoreMap.insert(faultCode, faultScore);
}

void GlobalStatic::initializeTrainStep(const int level)
{
    if (level == 1) {
         g_stepTrainedMap.insert("C1510101001", false);
         g_stepTrainedMap.insert("C1510101002", false);
         g_stepTrainedMap.insert("C1510101003", false);
         g_stepTrainedMap.insert("C1510101004", false);
         g_stepTrainedMap.insert("C1510102001", false);
         g_stepTrainedMap.insert("C1510102002", false);
         g_stepTrainedMap.insert("C1510102003", false);
         g_stepTrainedMap.insert("C1510102004", false);
         g_stepTrainedMap.insert("C1510102005", false);
         g_stepTrainedMap.insert("C1510102006", false);
         g_stepTrainedMap.insert("C1510102007", false);
         g_stepTrainedMap.insert("C1510102008", false);
         g_stepTrainedMap.insert("C1510102009", false);
         g_stepTrainedMap.insert("C1510102010", false);
         g_stepTrainedMap.insert("C1510102011", false);
         g_stepTrainedMap.insert("C1510102012", false);
         g_stepTrainedMap.insert("C1510102013", false);
         g_stepTrainedMap.insert("C1510102014", false);
         g_stepTrainedMap.insert("C1510102015", false);
         g_stepTrainedMap.insert("C1510102016", false);
         g_stepTrainedMap.insert("C1510102017", false);
         g_stepTrainedMap.insert("C1510102018", false);
         g_stepTrainedMap.insert("C1510102019", false);
         g_stepTrainedMap.insert("C1510103001", false);
         g_stepTrainedMap.insert("C1510103002", false);
         g_stepTrainedMap.insert("C1510103003", false);
         g_stepTrainedMap.insert("C1510103004", false);
         g_stepTrainedMap.insert("C1510103005", false);
         g_stepTrainedMap.insert("C1510103006", false);
         g_stepTrainedMap.insert("C1510103007", false);
         g_stepTrainedMap.insert("C1510103008", false);
         g_stepTrainedMap.insert("C1510103009", false);
         g_stepTrainedMap.insert("C1510103010", false);
    }

    if (level == 2) {
        g_stepTrainedMap.insert("C1510201001", false);
        g_stepTrainedMap.insert("C1510201002", false);
        g_stepTrainedMap.insert("C1510201003", false);
        g_stepTrainedMap.insert("C1510201004", false);
        g_stepTrainedMap.insert("C1510202001", false);
        g_stepTrainedMap.insert("C1510202002", false);
        g_stepTrainedMap.insert("C1510202003", false);
        g_stepTrainedMap.insert("C1510202004", false);
        g_stepTrainedMap.insert("C1510202005", false);
        g_stepTrainedMap.insert("C1510202006", false);
        g_stepTrainedMap.insert("C1510202007", false);
        g_stepTrainedMap.insert("C1510202008", false);
        g_stepTrainedMap.insert("C1510202009", false);
        g_stepTrainedMap.insert("C1510202010", false);
        g_stepTrainedMap.insert("C1510202011", false);
        g_stepTrainedMap.insert("C1510202012", false);
        g_stepTrainedMap.insert("C1510202013", false);
        g_stepTrainedMap.insert("C1510202014", false);
        g_stepTrainedMap.insert("C1510202015", false);
        g_stepTrainedMap.insert("C1510202016", false);
        g_stepTrainedMap.insert("C1510202017", false);
        g_stepTrainedMap.insert("C1510202018", false);
        g_stepTrainedMap.insert("C1510202019", false);
        g_stepTrainedMap.insert("C1510202020", false);
        g_stepTrainedMap.insert("C1510203001", false);
        g_stepTrainedMap.insert("C1510203002", false);
        g_stepTrainedMap.insert("C1510203003", false);
        g_stepTrainedMap.insert("C1510203004", false);
        g_stepTrainedMap.insert("C1510203005", false);
        g_stepTrainedMap.insert("C1510203006", false);
        g_stepTrainedMap.insert("C1510203007", false);
        g_stepTrainedMap.insert("C1510203008", false);
        g_stepTrainedMap.insert("C1510203009", false);
        g_stepTrainedMap.insert("C1510203010", false);
    }
}

void GlobalStatic::assignTrainStep(const QString code)
{
    if (g_stepTrainedMap.contains(code))
        g_stepTrainedMap[code] = true;
}

void GlobalStatic::resetTrainStep()
{
    QMap<QString, bool>::iterator iter;
    for (iter = g_stepTrainedMap.begin(); iter != g_stepTrainedMap.end(); iter++) {
        iter.value() = false;
    }
}

int GlobalStatic::getTrainedStepCount()
{
    int trained = 0;

    QMap<QString, bool>::iterator iter;
    for (iter = g_stepTrainedMap.begin(); iter != g_stepTrainedMap.end(); iter++) {
        if (iter.value())
            trained++;
    }

//    QMapIterator<QString, bool> it(g_stepTrainedMap);
//    while (it.hasNext()) {
//        it.next();
//        qDebug() << it.key() << ":" << it.value();
//    }

    return trained;
}

int GlobalStatic::getTotalStepCount()
{
    QStringList keys = g_stepTrainedMap.keys();
    return keys.count();
}

